/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.demo.controller.pay;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.demo.dto.WXpayDTO;
import com.demo.service.AliPayService;
import com.demo.util.AlipayUtils;
import com.yungouos.pay.wxpay.WxPay;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @authorleiliang
 * @date 2021/1/6 16:40
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/api/pay")
public class JHPayController {

    private final AlipayUtils alipayUtils;
    private final AliPayService alipayService;


    public static final String mchId = "1529637931";
    public static final String key = "499F61DB734C4BF39792A098C44FA80A";
    // 收银台支付结束后返回地址
    public static final String returnUrl = "127.0.0.1:8080/api/pay/return";
    public static final String notifyUrl = "/notify";

    /**
     * 扫码支付 返回二维码连接
     */
    @PostMapping(value = "/wxpay")
    @ResponseBody
    public ResponseEntity<String> toPayAsPc(@Validated @RequestBody WXpayDTO wxpayDTO) throws Exception {

        //String result = WxPay.nativePay("1556267522890", "0.1", mchId, "测试", null, null, null, null, null, null, null, key);
        String result = null;
        if (wxpayDTO != null) {
            result =  WxPay.nativePay(String.valueOf(IdWorker.getId()), wxpayDTO.getTotal_fee(), mchId, wxpayDTO.getBody(), null, null, notifyUrl, returnUrl, null, null, null, key);
        }
        System.out.println("扫码支付 结果：" + result);
        return ResponseEntity.ok(result);
    }



    /**
     * 扫码支付 返回二维码连接
     */
    @PostMapping(value = "/return")
    @ResponseBody
    public void returnObject(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println("同步回调 结果：" + parameterMap);
    }


}
